<?php
namespace app\api\controller;
use think\Controller;
use think\Db;
use app\api\model\News as news_list;
use app\api\model\Talent as talent_list;
class Policy extends Index{
    public function index(){
        $area_name = input('get.city');
        $type = input('get.type');
        if($type == 'city'){
            $map['p.area1'] = $area_name;
        }else{
            $map['p.area2'] = $area_name;
        }
        $map['p.status']=2;
        $res2 = Db::table('zgh_policy')->alias('p')
            ->field('p.id,p.area1,p.area2,p.content,p.sort,py.t_id,py.name,py.pid,py.sort as py_sort')
            ->join('zgh_policy_type py','p.sort=py.t_id')
            ->where($map)
            ->order('py.sort desc')
            ->select();
        $news_type = Db::table('zgh_policy_type')->where('status=1')->select();
        if(!isset($news_type)){
            $this->json('1000','数据查询失败');
        }
        $type2='';
        $type1='';
        foreach ($news_type as $k1 => $v1) {
            if($v1['pid']==0){
                $type1[]=$v1;
            }else{
                $type2[]=$v1;
            }
        }
        foreach ($res2 as $k1 => $v1) {
            foreach ($type2 as $k2 => $v2) {
                if($res2[$k1]['sort'] == $type2[$k2]['t_id']){
                    $type2[$k2]['content'][] = $v1;
                }
            }
        }
        foreach ($type2 as $k => $v) {
            if(empty($type2[$k]['content']) ){
                unset($type2[$k]);
            }
        }
        foreach ($type1 as $k1 => $v1) {
            foreach ($type2 as $k2 => $v2) {
                if($type2[$k2]['pid'] == $type1[$k1]['t_id']){
                    $type1[$k1]['son'][] = $v2;
                }
            }
        }
        $this->_jsonp('200','成功',$type1);
        //dump($res2);
    }
    /*
     * 随机5条知产信息
     * */
    public function info(){
        $news=new news_list('policy');
        $where['status']=2;
        $limit=5;
        $data=$news->rand_list($where,"content",$limit);
        if(!empty($data)){
            $this->_jsonp('200','查询成功',$data);
        }else{
            $this->_jsonp('1000','查询失败',null);
        }
    }
    /*
     * 政务工作业务指南列表
     * sort为类型分类
     * */
    public function business(){
        $where['sort'] = input('get.sort');
        $where['status']=2;
        $news=new news_list('business');
        $data=$news->rand_list($where,"*",10,'time desc');
        if(!empty($data)){
            $this->_jsonp('200','查询成功',$data);
        }else{
            $this->_jsonp('1000','查询失败',null);
        }
    }
    public function business_detail(){
        $id = input('get.id');
        $news=new news_list('business');
        $data=$news->news_detail($id);
        if(!empty($data)){
            $this->_jsonp('200','查询成功',$data);
        }else{
            $this->_jsonp('1000','查询失败',null);
        }
    }
	/*
     * 后台政策信息地址
     * 
     * */
	public function area(){
		$pid = input('get.pid');
		if($pid==0){
			$this->_jsonp('1000','查询类别失败',null);
		}
		$data=Db::name('policy_area')->where('pid',$pid)->where('status',1)->select();
		if(!empty($data)){
            $this->_jsonp('200','查询地址成功',$data);
        }else{
            $this->_jsonp('1000','查询地址失败',null);
        }
	}
	public function area_name(){
		$pid = input('get.pid');
		
		
		$res=Db::name('policy_area')->where('name',$pid)->where('status',1)->value('a_id');
		$data=Db::name('policy_area')->where('pid',$res)->where('status',1)->select();
		
		if(!empty($data)){
            $this->_jsonp('200','查询地址成功',$data);
        }else{
            $this->_jsonp('1000','查询地址失败',null);
        }
	}
	/*
     * 后台政策信息地址
     * 
     * */
	public function type(){
		$pid = input('get.pid');
		if($pid==0){
			$this->_jsonp('1000','查询类别失败',null);
		}
		$data=Db::name('policy_type')->where('pid',$pid)->where('status',1)->select();
		if(!empty($data)){
            $this->_jsonp('200','查询类别成功',$data);
        }else{
            $this->_jsonp('1000','查询类别失败',null);
        }
	}
	/*
     * 后台政策信息列表
     * 
     * */
	 public function policy_list(){
		 $search = htmlspecialchars(trim(input('get.search')));
		 $type = input('get.type');//父级分类
		 $sort = input('get.sort');//子级分类
		 $area = input('get.area');//城市分类
		 $city = input('get.city');//区镇分类
		 if($sort){
			 $where['p.sort']=$sort;
		 }else{
			 switch ($type)
				{
				case 0:
				  $where['p.sort']=array('neq',0);
				  break;
				case 1:
				  $where['p.sort']=array('in','9,10,11,12,13,14');
				  break;
				case 2:
				  $where['p.sort']=array('in','15,16,17,18,19,20');
				  break;
			    case 3:
				  $where['p.sort']=array('in','3,28');
				  break;
				case 4:
				  $where['p.sort']=array('in','21,22,23');
				  break;
				  case 5:
				  $where['p.sort']=array('in','5,29');
				  break;
				  case 6:
				  $where['p.sort']=array('in','6,30');
				  break;
				case 7:
				  $where['p.sort']=array('in','24,25,26,27');
				  break;
				  case 8:
				  $where['p.sort']=array('in','8,31');
				  break;
				default:
				  $where['p.sort']=$type;
				}
		 }
		 if($city){
			$map['a_id']=$city;
			$where['p.area2']=Db::name('policy_area')->where($map)->value('name');
		 }else{
			 if($area!=0){
				$map['a_id']=$area;
				$where['p.area1']=Db::name('policy_area')->where($map)->value('name'); 
			 }
		 }
		 $limit=10;
		 $where['p.status']=array('neq',3);//3代表删除
		 $where['p.content'] = array('like', "%$search%");
		 $page = (isset($_GET['page']) && !empty($_GET['page'])) ? $_GET['page'] : 1;
		 $count=Db::name('policy')->alias('p')->where($where)->count();
		 $res2 = Db::name('policy')->alias('p')
            ->field('p.id,p.time,p.paper,p.stime,u.username,p.status,p.area1,p.area2,p.content,p.sort,py.t_id,py.name,py.pid,py.sort as py_sort')
            ->join('zgh_policy_type py','p.sort=py.t_id')
			->join('zgh_user u','p.uid=u.uid')
            ->where($where)
            ->order('p.id desc')
            ->limit($limit)
            ->page($page)->select();
		$res = model('Common')->page_data($count, $page, $limit);
        $res['count'] = $count;
        $res['data'] = $res2;
        if (isset($res) && !empty($res)) {
            $this->_jsonp('200', '列表数据获取成功', $res);
        } else {
            $this->_jsonp('1000', '列表数据获取失败', null);
        }
	 }
	 //添加政策信息
	 public function policy_add(){
		$data["content"]=htmlspecialchars(trim(input('post.content')));
		$data["status"]=input('post.status');
		$sort=input('post.sort');
		$type=input('post.type');
		$area=input('post.area');
		$data["area1"]=Db::name('policy_area')->where('a_id',$area)->value('name');
		$city=input('post.city');
		if($city){
			$data["area2"]=Db::name('policy_area')->where('a_id',$city)->value('name');
		}
		if($type){
			$data["sort"]=$type;
		}else{
			$data["sort"]=$sort;
		}
		$data["paper"]=input('post.paper');
		$data["stime"]=input('post.news_title');
		$data["uid"]=input('post.uid');
		$data["time"]=time();
		$flag=Db::name('policy')->insert($data);
		if($flag){
			$this->json('200', '列表数据添加成功', null);	
		}else{
			$this->json('1000', '列表数据添加失败', null);	
		}		
	 }
	 //修改政策信息
	 public function policy_edit(){
		 $id=input('post.id');
		$data["content"]=htmlspecialchars(trim(input('post.content')));
		$data["status"]=input('post.status');
		$sort=input('post.sort');
		$type=input('post.type');
		$area=input('post.area');
		$data["area1"]=$area;
		$city=input('post.city');
		if($city){
			$data["area2"]=$city;
		}
		if($type){
			$data["sort"]=$type;
		}else{
			$data["sort"]=$sort;
		}
		$data["paper"]=input('post.paper');
		$data["stime"]=input('post.news_title');
		$data["uid"]=input('post.uid');
		
		$flag=Db::name('policy')->where('id',$id)->update($data);
		if($flag){
			$this->json('200', '列表数据添加成功', null);	
		}else{
			$this->json('1000', '列表数据添加失败', null);	
		}		
	 }
	 //政策信息详情
	 public function policy_detail(){
		$id=input('get.id');
		$res=Db::name('policy')->where('id',$id)->find();
		$res['pid']=Db::name('policy_type')->where('t_id',$res['sort'])->value('pid');
		if($res){
			$this->_jsonp('200', '数据查询成功', $res);	
		}else{
			$this->_jsonp('1000', '数据查询失败', null);	
		}		
	 }
	 public function status_edit()
    {
        $true = auth();
        if ($true == 2) {
            $id = input('get.id');
            $data['status'] = input('get.status');
            $news_list = new news_list('policy');
            $data = $news_list->news_del($id, $data);
            if ($data['res']) {
                action_log('政策信息修改状态', 'log_admin',1);
                $this->_jsonp('200', $data['name'] . '成功', null);
            } else {
                action_log('政策信息修改状态', 'log_admin');
                $this->_jsonp('1000', $data['name'] . '失败', null);
            }
        } else if($true == 1) {
            $this->_jsonp('1000', '您没有权限', null);
        }else{
			$this->_jsonp('1000','您未登录',null);
		}

    }
}